package com.loveshare.love.rest;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.loveshare.common.util.HttpClientIpUtil;
import com.loveshare.common.util.HttpRequest;
import com.loveshare.love.bean.SimpleLoveShow;
import com.loveshare.love.entity.LoveShow;
import com.loveshare.love.service.LoveShowService;

/**
 * 
 * ClassName: LoveShowRest 
 * @Description: 表白展示控制器
 * @author 爱分享
 * @date 2017年12月20日
 */
@RestController
@RequestMapping(value="love")
public class MobileLoveShowRest {
	
	@Autowired
	private LoveShowService loveShowService;
	
	/**
	 * 添加表白墙
	 * @return
	 */
	@RequestMapping(value="addLoveShow",method=RequestMethod.POST)
	public boolean addLoveShow(@RequestParam String nickname,
			@RequestParam String content,HttpServletRequest request){
		//特殊词过滤
		if(content.indexOf("操")!=-1||
		content.indexOf("傻逼")!=-1||
		content.equalsIgnoreCase("sql")==true||
		content.equalsIgnoreCase("delete")==true||
		content.equalsIgnoreCase("update")==true||
		content.equalsIgnoreCase("sb")==true||
		content.equalsIgnoreCase("drop")==true){
			return false;
		}
		if(nickname.length()>20){
			return false;
		}
		String ip=HttpClientIpUtil.getIP(request);
		int count=loveShowService.getLoveCount(ip);
		if(count>30){
			return false;
		}
		LoveShow loveShow=new LoveShow();
		loveShow.setContent(content);
		loveShow.setIp(ip);
		loveShow.setIpregion(HttpRequest.getIpRegion(ip));
		loveShow.setNickname(nickname);
		loveShow.setTitle(nickname);
		loveShow.setLoveId(UUID.randomUUID().toString());
		loveShowService.addLoveShow(loveShow);
		return true;
	}
	
	/**
	 * 添加表白墙
	 * @return
	 */
	@RequestMapping(value="test",method=RequestMethod.POST)
	public boolean test(@RequestParam String nickname,
			@RequestParam String content,HttpServletRequest request){
		//特殊词过滤
		if(content.indexOf("操")!=-1||
		content.indexOf("傻逼")!=-1||
		content.equalsIgnoreCase("sql")==true||
		content.equalsIgnoreCase("delete")==true||
		content.equalsIgnoreCase("update")==true||
		content.equalsIgnoreCase("sb")==true||
		content.equalsIgnoreCase("drop")==true){
			return false;
		}
		if(nickname.length()>20){
			return false;
		}
		String ip=HttpClientIpUtil.getIP(request);
		int count=loveShowService.getLoveCount(ip);
		if(count>30){
			return false;
		}
		LoveShow loveShow=new LoveShow();
		loveShow.setContent(content);
		loveShow.setIp(ip);
		loveShow.setIpregion(HttpRequest.getIpRegion(ip));
		loveShow.setNickname(nickname);
		loveShow.setTitle(nickname);
		loveShow.setLoveId(UUID.randomUUID().toString());
		loveShowService.addLoveShow(loveShow);
		return true;
	}
	
	/**
	 * 加载更多表白
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/getLoveShows",method=RequestMethod.GET)
	public List<SimpleLoveShow> index(@RequestParam int page,ModelMap map){
		List<SimpleLoveShow> loveShows=loveShowService.getLoveShows(page,36);
		return loveShows;
	}
}
